package jude.java.money.test.init;

import java.util.Calendar;

import jude.java.framework.test.SpringTestCase;
import jude.java.money.beans.WeakPassword;
import jude.java.money.service.GlobalService;
import jude.java.money.spring.Services;
import jude.java.util.DateUtils;

@SuppressWarnings("unused")
public class InitWeakPassword extends SpringTestCase {
	
	private int index = 0;

	public void testInitWeakPasswords() {
		Services.getGlobalService().update("delete from " + WeakPassword.class.getName());
		//特殊
		i(0, "123456", "12345678", "666", "888", "999", "666666", "888888", "999999", "123", "1234");
		i(0, "abc", "abcd");
		i(1, "ababab", "abcdef", "abcabc");
		i(2, "520", "admin", "1314", "112233");
		i(5, "test", "iloveyou", "iloveu", "abc123", "Admin");
		//连续
		i(1, '0', '9');
		i(4, '9', '0');
		//重复字母
		i(4, '0', '9', 3);
		i(4, '0', '9', 4);
		i(3, '0', '9', 5);
		i(1, '0', '9', 6);
		i(4, '0', '9', 7);
		i(3, '0', '9', 8);
		i(4, 'a', 'e', 6);
		i(6, 'f', 'z', 6);
		i(6, 'A', 'Z', 6);
		i(7, 'a', 'z', 8);
		//重复两个字母
		i(2, 6, '0', '9');
		i(5, 8, '0', '9');
		//数字
		i(3, "", 0, 99);
		i(4, "0", 1, 9);
		//年份
		i(6, "", 1971, 1988);
		i(7, "", 1989, 2012);
		i(10, "", 1949, 1970);
		//年月
//		i(11, 1949, 1975);
//		i(10, 1976, 2000);
//		i(9, 2001, 2012);
		//年月日, yyMMdd
//		i2(11, 1949, 1975);
//		i2(10, 1970, 1979);
		i2(9, 1980, 1989);
//		i2(10, 1990, 1999);
//		i2(11, 2000, 2010);
		//单词
		i(8, "abgrtyu access action albert alexis amanda amateur andrea andrew angela angels animal anthony apollo apples arsenal arthur asdfgh asdfgh ashley august austin badboy bailey banana barney baseball batman beaver beavis bigdaddy bigdog birdie bitches biteme blazer blonde blondes bond007 bonnie booboo booger boomer boston brandon brandy braves brazil bronco broncos bulldog buster butter butthead calvin camaro cameron canada captain carlos carter casper charles charlie cheese chelsea chester chicago chicken cocacola coffee college compaq computer cookie cooper corvette cowboy cowboys crystal dakota dallas daniel danielle debbie dennis diablo diamond doctor doggie dolphin dolphins donald dragon dreams driver eagle1 eagles edward einstein erotic extreme falcon fender ferrari firebird fishing florida flower flyers football forever freddy freedom gandalf gateway gators gemini george giants ginger golden golfer gordon gregory guitar gunner hammer hannah hardcore harley heather helpme hockey hooters horney hotdog hunter hunting iceman iloveyou internet iwantu jackie jackson jaguar jasmine jasper jennifer jeremy jessica johnny johnson jordan joseph joshua junior justin killer knight ladies lakers lauren leather legend letmein little london lovers maddog madison maggie magnum marine marlboro martin marvin master matrix matthew maverick maxwell melissa member mercedes merlin michael michelle mickey midnight miller mistress monica monkey monkey monster morgan mother mountain muffin murphy mustang naked nascar nathan naughty ncc1701 newyork nicholas nicole nipple nipples oliver orange packers panther panties parker password password password1 password12 password123 patrick peaches peanut pepper phantom phoenix player please pookie porsche prince princess private purple pussies qazwsx qwerty qwertyui rabbit rachel racing raiders rainbow ranger rangers rebecca redskins redsox redwings richard robert rocket rosebud runner russia samantha sammy samson sandra saturn scooby scooter scorpio scorpion secret sexsex shadow shannon shaved sierra silver skippy slayer smokey snoopy soccer sophie spanky sparky spider squirt srinivas startrek starwars steelers steven sticky stupid success summer sunshine superman surfer swimming sydney taylor tennis teresa tester testing theman thomas thunder thx1138 tiffany tigers tigger tomcat topgun toyota travis trouble trustno1 tucker turtle twitter united vagina victor victoria viking voodoo voyager walter warrior welcome whatever william willie wilson winner winston winter wizard xavier yamaha yankee yankees yellow zxcvbn zxcvbnm".split(" "));
	}
	
	/** 年月日 */
	private void i2(int level, int startYear, int endYear) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, startYear);
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		do {
			i(level, DateUtils.toString(cal.getTime(), "yyMMdd"));
			cal.add(Calendar.DAY_OF_MONTH, 1);
		} while (cal.get(Calendar.YEAR) <= endYear);
	}
	
	private void i(int level, int startYear, int endYear) {
		for (int i = startYear; i <= endYear; i++) {
			i(level, i + "0", 1, 9);
			i(level, String.valueOf(i), 1, 12);
		}
	}
	
	private void i(int level, String perfix, int start, int end) {
		for (int i=start; i<=end; i++) {
			String pass = perfix + i;
			i(level, pass);
		}
	}
	
	private void i(int level, char startChar, char endChar) {
		char[] cs = new char[Math.abs(endChar - startChar) + 1];
		if (endChar >= startChar) {
			for (char c = startChar; c <= endChar; c++) {
				for (char d = c; d <= endChar; d++) {
					cs[d - c] = d;
					i(level, new String(cs, 0, d - c + 1));
				}
			}
		} else {
			for (char c = startChar; c >= endChar; c--) {
				for (char d = c; d >= endChar; d--) {
					cs[c - d] = d;
					i(level, new String(cs, 0, c - d + 1));
				}
			}
		}
//		i(level, new String(cs));
	}
	
	private void i(int level, char startChar, char endChar, int repeat) {
		char[] cs = new char[repeat];
		for (char c = startChar; c <= endChar; c++) {
			for (int i = 0; i < cs.length; i++) {
				cs[i] = c;
			}
			i(level, new String(cs));
		}
	}
	
	/**
	 * 两个两个重复
	 * */
	private void i(int level, int maxLength, char startChar, char endChar) {
		char[] cs = new char[Math.abs(startChar - endChar) + 1];
		if (endChar >= startChar) {
			for (char c = startChar; c <= endChar; c ++) {
				cs[c - startChar] = c;
			}
		} else {
			for (char c = endChar; c >= startChar; c --) {
				cs[endChar - c] = c;
			}
		}
		char[] cc = new char[maxLength];
		for (int i = 0; i < cs.length; i++) {
			for (int j = 0; j < cs.length; j++) {
				char a = cs[i], b = cs[j];
				for (int k = 0; k < maxLength;) {
					cc[k++] = a;
					cc[k++] = b;
				}
				i(level, new String(cc));
			}
		}
	}
	
	private void i(int level, String... pass) {
		for (int i = 0; i < pass.length; i++) {
			i(new WeakPassword(pass[i], level));
		}
	}
	
	private void i(WeakPassword password) {
		if (password.getLevel() > 5)
			return;
		GlobalService service = Services.getGlobalService();
		WeakPassword p = service.get(WeakPassword.class, "password", password.getPassword());
		if (p == null) {
			service.insert(password);
			System.out.println(index++ + " " + password.getPassword());
		}
	}
}
